﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using Landpy.LogAnalyzer.Common;
using Landpy.LogAnalyzer.Configuration;

namespace Landpy.LogAnalyzer.Log
{
    internal class LogReader : ILogReader
    {
        private readonly List<LogColumn> columns;
        private readonly string globalFilterEndFlag;
        private readonly string globalFilterStartFlag;
        private readonly string logFileName;

        public LogReader(string logFileName)
        {
            this.logFileName = logFileName;
            columns = ConfigurationSession.GetSession().Setting.Columns;
            globalFilterStartFlag = ConfigurationSession.GetSession().Setting.GlobalFilterStartFlag;
            globalFilterEndFlag = ConfigurationSession.GetSession().Setting.GlobalFilterEndFlag;
        }

        #region ILogReader Members

        public List<LogRow> GetRows()
        {
            var logRows = new List<LogRow>();
            Regex regex = null;
            Match match = null;
            if (File.Exists(ConfigurationParam.TempLogFileName))
            {
                File.Delete(ConfigurationParam.TempLogFileName);
            }
            File.Copy(logFileName, ConfigurationParam.TempLogFileName);
            using (
                var stream = new FileStream(ConfigurationParam.TempLogFileName, FileMode.Open, FileAccess.Read,
                                            FileShare.Delete))
            {
                using (TextReader textReader = new StreamReader(stream))
                {
                    string lineString = String.Empty;
                    bool inProcess = false;
                    var content = new StringBuilder();
                    while (!String.IsNullOrEmpty(lineString = textReader.ReadLine()))
                    {
                        #region Start

                        if (!inProcess)
                        {
                            regex = new Regex(globalFilterStartFlag);
                            match = regex.Match(lineString);
                            if (!String.IsNullOrEmpty(match.Value))
                            {
                                inProcess = true;
                                content.Append(match.Value);
                            }
                        }
                        else
                        {
                            regex = new Regex(globalFilterEndFlag);
                            match = regex.Match(lineString);
                            if (!String.IsNullOrEmpty(match.Value))
                            {
                                inProcess = false;
                                LogRow logRow = GenerateLogRow(content);
                                logRows.Add(logRow);
                                content = new StringBuilder();
                            }
                            content.Append(lineString.Substring(lineString.IndexOf("Verbose") + 7).Trim());
                        }

                        #endregion
                    }

                    textReader.Close();
                }
                stream.Close();
            }
            return logRows;
        }

        #endregion

        private LogRow GenerateLogRow(StringBuilder content)
        {
            var logRow = new LogRow();
            string contentString = content.ToString();
            foreach (LogColumn logColumn in columns)
            {
                int startIndex = contentString.IndexOf(logColumn.Filter);
                string tmpSection = contentString.Substring(startIndex);
                string columnValue = String.Empty;
                if (tmpSection.IndexOf(";") != -1)
                {
                    columnValue = tmpSection.Substring(0, tmpSection.IndexOf(";"));
                }
                else
                {
                    columnValue = tmpSection;
                }
                if (String.IsNullOrEmpty(columnValue))
                {
                    logRow[logColumn.HeaderText] = ConfigurationParam.EmptyColumnValue;
                }
                else
                {
                    logRow[logColumn.HeaderText] = columnValue.Replace(logColumn.Filter, String.Empty);
                }
            }
            return logRow;
        }
    }
}